<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*,QuizPackage.*"%>
<%@ page import="message.*"%>
<%@ page import="social.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Quizzes</title>
</head>
<body>

<div id="container"><!-- Header Div -->
<div id="header" style="background-color: #3B5998; color: white; font: Tahoma; float: center ">
<h1 style="margin-bottom: 0; text-align: center;"><!--User welcome message--> <%
 	SocialDBC db = (SocialDBC) request.getServletContext()
 			.getAttribute("database");
 	QuizDBC qdb = (QuizDBC) request.getServletContext().getAttribute(
 			"quizDatabase");

 	String profile = (String) request.getParameter("profileName");
 	String profileName;
 	if (profile == null) {
 		profileName = (String) session.getAttribute("profileName");
 	} else {
 		profileName = profile;
 	}

 	String username = (String) session.getAttribute("username");

 	if (username == null) {
 		response.sendRedirect("homepage.jsp");
 	}

 	//System.out.println(String.format("username:%s profileName:%s",
 			//username, profileName));
	
 	out.println("<img src=\"Nuvola_apps_korganizer.png\" alt=\"Smiley face\" height=\"45\" width=\"45\" />");
 	if (profileName.equals(username)) {
 		out.println(String.format(
 				"<style=\"text-align: center\"> Welcome %s!",
 				profileName));
 	} else {
 		out.println(String.format(
 				"<style=\"text-align: center\"> %s's Profile </br>",
 				profileName));
 	}
 %> 
 
 </h1>
</div>

 <!-- Search Box -->
<div id="header" style="background-color: #3B5998; color: white; font: Tahoma ">
<form method="post" action="SearchServlet"><input type="text"
	name="queryUser" /> <input type="submit" value="Search" /></form>
</div>

<div id="buffer"style="background-color: #3B5998; width: 100%; height: 10px; float: left">
</div>

<div id="buttonContainer" style="background-color: #3B5998; color: white; font: Tahoma ">
<%
	//Log out button
	out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 6%; float: left\">");
	out.println("<form action=\"LogoutServlet\" method=\"post\">"
			+ "<input type=\"submit\" value=\"Log out\"/> </form>");
	out.println("</div>");

	//Log out button
	out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 8%; float: left\">");
	out.println("<form action=\"CreateQuiz.jsp\" method=\"post\">"
			+ "<input type=\"submit\" value=\"Create Quiz\"/> </form>");
	out.println("</div>");

	//Quiz List button
	out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 7%; float: left\">");
	out.println("<form action=\"quizList.jsp\" method=\"post\">"
			+ "<input type=\"submit\" value=\"Quiz List\"/> </form>");
	out.println("</div>");

	//<!-- Mailbox Link --> 
	if (profileName.equals(username)) {
		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 7%; float: left\">");
		out.println("<form method = \"post\" action = \"mailbox.jsp\">");
		out.println("<input type = \"submit\" value =\"Mailbox\"/>");
		out.println("</form>");
		out.println("</div>");

		if (db.checkAdmin(username)) {
			//Back to profile button
			out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 10%; float: left\">");
			out.println("<form method = \"post\" action = \"user-profile.jsp?profileName="
					+ username + "\">");
			out.println("<input type = \"submit\" value =\"Back to Profile\"/>");
			out.println("</form>");
			out.println("</div>");
		} else {
			out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 72%; float: left\">");
			out.println("<form method = \"post\" action = \"user-profile.jsp?profileName="
					+ username + "\">");
			out.println("<input type = \"submit\" value =\"Back to Profile\"/>");
			out.println("</form>");
			out.println("</div>");
		}

	} else {
		//Back to profile button
		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 10%; float: left\">");
		out.println("<form method = \"post\" action = \"user-profile.jsp?profileName="
				+ username + "\">");
		out.println("<input type = \"submit\" value =\"Back to Profile\"/>");
		out.println("</form>");
		out.println("</div>");
	}
%>

<%
	if (db.checkAdmin(username) && !profileName.equals(username)) {
		//Remove User Button
		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 10%; float: left\">");
		out.println(String
				.format("<form action=\"AdminServlet\" method=\"post\">"
						+ "<input type=\"submit\" value=\"Remove User\">"
						+ "<input name=\"action\" type=\"hidden\" value=\"removeUser\"/>"
						+ "<input name=\"userAction\" type=\"hidden\" value=\"%s\"/>"
						+ "</form>", profileName));
		out.println("</div>");

		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 59%; float: left\">");
		if (!db.checkAdmin(profileName)) {
			//Promote to Admin button
			out.println(String
					.format("<form action=\"AdminServlet\" method=\"post\">"
							+ "<input type=\"submit\" value=\"Promote to Admin\">"
							+ "<input name=\"action\" type=\"hidden\" value=\"promoteAdmin\"/>"
							+ "<input name=\"userAction\" type=\"hidden\" value=\"%s\"/>"
							+ "</form>", profileName));
			out.println("</div>");
		} else {

			//Remove Admin button
			out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 59%; float: left\">");
			out.println(String
					.format("<form action=\"AdminServlet\" method=\"post\" float: right>"
							+ "<input type=\"submit\" value=\"Remove Admin\">"
							+ "<input name=\"action\" type=\"hidden\" value=\"removeAdmin\"/>"
							+ "<input name=\"userAction\" type=\"hidden\" value=\"%s\"/>"
							+ "</form>", profileName));
			out.println("</div>");
		}
		//out.println("</div>");
	}
	if (db.checkAdmin(username) && (profileName.equals(username))) {
		//Site Statistics button
		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 10%; float: left\">");
		out.println(String
				.format("<form action=\"statistics.jsp\" method=\"post\" float: right>"
						+ "<input type=\"submit\" value=\"Site Statistics\">"
						+ "</form>"));
		out.println("</div>");

		//Make announcement button
		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 52%; float: left\">");
		out.println(String
				.format("<form action=\"create-announcement.jsp\" method=\"post\" float: right>"
						+ "<input type=\"submit\" value=\"Make Announcement\">"
						+ "<input name=\"userAction\" type=\"hidden\" value=\"%s\"/>"
						+ "</form>", profileName));
		out.println("</div>");
	}
%>

</div>

<div id="buffer"style="background-color: #3B5998; width: 100%; height: 10px; float: left">
</div>

<h1 style= "text-align: center; background-color: #FDEEF4""> Quiz List </h1>

<!-- Search For Tag -->
<form method="post" action="quizList.jsp">
<p>Search for a Tag</p>
<input type="text" name="tag" /> 
<input type="submit" value="Search For Tag" />
</form>

<!-- Quiz List -->
<div id="quiz1"style="text-align:center; width: 33%; float: left">
<%
QuizDBC dbc = new QuizDBC();
//ArrayList<Integer> ids = dbc.getQuizList();
if(request.getParameter("tag")==null)
{
	for(int i=0 ; i < Quiz.quizCategories.length/3 ; i++)
	{
		out.println("<h3>"+Quiz.quizCategories[i]+"</h3>");
		ArrayList<Integer> ids = dbc.getQuizListByCategory(Quiz.quizCategories[i]);

		for(int j = 0 ; j < ids.size() ; j++)
		{
			String name = dbc.getStringByID(ids.get(j),"name");
			String creator = dbc.getStringByID(ids.get(j),"creator");
			out.println("<p> <a href=\"StartQuiz?quizid="+ids.get(j)+"\" >"+name+"</a></p>");
		}
	}
}else{
	out.println("<h3>Quizzes tagged with \""+request.getParameter("tag")+"\"</h3>");
	ArrayList<Integer> ids = dbc.getQuizListByTag(request.getParameter("tag"));

	for(int j = 0 ; j < ids.size()/3 ; j++)
	{
		String name = dbc.getStringByID(ids.get(j),"name");
		String creator = dbc.getStringByID(ids.get(j),"creator");
		out.println("<p> <a href=\"StartQuiz?quizid="+ids.get(j)+"\" >"+name+"</a></p>");
	}
}
%>
</div>

<div id="quiz1"style="text-align:center; width: 33%; float: left">
<%
if(request.getParameter("tag")==null)
{
	for(int i=Quiz.quizCategories.length/3 ; i < 2*Quiz.quizCategories.length/3 ; i++)
	{
		out.println("<h3>"+Quiz.quizCategories[i]+"</h3>");
		ArrayList<Integer> ids = dbc.getQuizListByCategory(Quiz.quizCategories[i]);

		for(int j = 0 ; j < ids.size() ; j++)
		{
			String name = dbc.getStringByID(ids.get(j),"name");
			String creator = dbc.getStringByID(ids.get(j),"creator");
			out.println("<p> <a href=\"StartQuiz?quizid="+ids.get(j)+"\" >"+name+"</a></p>");
		}
	}
}else{
	//out.println("<h3>Quizzes tagged with \""+request.getParameter("tag")+"\"</h3>");
	ArrayList<Integer> ids = dbc.getQuizListByTag(request.getParameter("tag"));

	for(int j = ids.size()/3 ; j < 2*ids.size()/3 ; j++)
	{
		String name = dbc.getStringByID(ids.get(j),"name");
		String creator = dbc.getStringByID(ids.get(j),"creator");
		out.println("<p> <a href=\"StartQuiz?quizid="+ids.get(j)+"\" >"+name+"</a></p>");
	}
}
%>
</div>

<div id="quiz1"style="text-align:center; width: 33%; float: left">
<%
if(request.getParameter("tag")==null)
{
	for(int i=2*Quiz.quizCategories.length/3 ; i < Quiz.quizCategories.length ; i++)
	{
		out.println("<h3>"+Quiz.quizCategories[i]+"</h3>");
		ArrayList<Integer> ids = dbc.getQuizListByCategory(Quiz.quizCategories[i]);

		for(int j = 0 ; j < ids.size() ; j++)
		{
			String name = dbc.getStringByID(ids.get(j),"name");
			String creator = dbc.getStringByID(ids.get(j),"creator");
			out.println("<p> <a href=\"StartQuiz?quizid="+ids.get(j)+"\" >"+name+"</a></p>");
		}
	}
}else{
	//out.println("<h3>Quizzes tagged with \""+request.getParameter("tag")+"\"</h3>");
	ArrayList<Integer> ids = dbc.getQuizListByTag(request.getParameter("tag"));

	for(int j = 2*ids.size()/3 ; j < ids.size() ; j++)
	{
		String name = dbc.getStringByID(ids.get(j),"name");
		String creator = dbc.getStringByID(ids.get(j),"creator");
		out.println("<p> <a href=\"StartQuiz?quizid="+ids.get(j)+"\" >"+name+"</a></p>");
	}
}
%>
</div>

</body>
</html>
